package com.dongdongshop.seller.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.dubbo.common.json.JSONArray;
import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.operator.pojo.*;
import com.dongdongshop.operator.service.ItemCatService;
import com.dongdongshop.operator.service.SpecificationService;
import com.dongdongshop.operator.service.TypeTemplateService;
import com.dongdongshop.seller.pojo.TbGoods;
import com.dongdongshop.seller.pojo.TbGoodsDesc;
import com.dongdongshop.seller.pojo.TbItem;
import com.dongdongshop.seller.service.GoodsService;
import com.dongdongshop_util.OSSutils;
import com.dongdongshop_util.Result;
import com.dongdongshop_util.ResultEnum;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("goods")
public class GoodsController {

    private final Logger logger = LoggerFactory.getLogger(GoodsController.class);

    //调用顶级分类表
    @Reference(check = false)
    private ItemCatService itemCatService;
    //模板
    @Reference(check = false)
    private TypeTemplateService typeTemplateService;
    //商品表service
    @Reference(check = false)
    private GoodsService goodsService;
    //规格表
    @Reference(check = false)
    private SpecificationService specificationService;



    //跳转到新增商品
    @RequestMapping("toGoodsEdit")
    public String toGoodsEdit(Model model){
        List<TbItemCat> itemCatList = itemCatService.getItemCat(null);
        model.addAttribute("itemCatList",itemCatList);
        return "admin/goods_edit";
    }
    //跳转到商品展示页
    @RequestMapping("toGoods")
    public String toGoods(){
        return "admin/goods";
    }

    //获取商品表goods，，，根据登录用户id查询
    @ResponseBody
    @RequestMapping("getGoodsBySellerId")
    public Result getGoodsBySellerId(Integer state,String name){
        try {
            //登录用户id
            TbSeller seller = (TbSeller) SecurityUtils.getSubject().getPrincipal();
            List<TbGoods> goodsList = goodsService.getGoodsBySellerId(seller.getSellerId(),state,name);
            return Result.success(ResultEnum.SUCCESS,goodsList);
        } catch (Exception e){
            logger.error(String.valueOf(ResultEnum.FAIT),e);
            return Result.response(ResultEnum.FAIT);
        }
    }

    //根据父级id获取二级分类
    @ResponseBody
    @RequestMapping("getTow")
    public Result getTow(Long parentId){
        try {
            List<TbItemCat> itemCat = itemCatService.getItemCat(parentId);
            return Result.success(ResultEnum.SUCCESS,itemCat);
        } catch (Exception e){
            logger.error(String.valueOf(ResultEnum.FAIT),e);
            return Result.response(ResultEnum.FAIT);
        }
    }
    //获取模板id
    @ResponseBody
    @RequestMapping("getTypeIdById")
    public Result getTypeIdById(Long id){
        try {
            TbItemCat itemCatById = itemCatService.getItemCatById(id);
            return Result.success(ResultEnum.SUCCESS,itemCatById);
        } catch (Exception e){
            logger.error(String.valueOf(ResultEnum.FAIT),e);
            return Result.response(ResultEnum.FAIT);
        }
    }
    //获取模板
    @ResponseBody
    @RequestMapping("selectTypeTemplateById")
    public Result selectTypeTemplateById(Long id){
        try {
            TbTypeTemplate typeTemplateById = typeTemplateService.getTypeTemplateById(id);
            return Result.success(ResultEnum.SUCCESS,typeTemplateById);
        } catch (Exception e){
            logger.error(String.valueOf(ResultEnum.FAIT),e);
            return Result.response(ResultEnum.FAIT);
        }
    }
    //增加商品表
    @ResponseBody
    @RequestMapping("addGoodsAndGoodsDesc")
    public Result addGoodsAndGoodsDesc(TbGoods goods, TbGoodsDesc goodsDesc,@RequestParam("itemList")String itemList){
        try {
            goods.setIsDelete("0");
            goods.setAuditStatus("0");
            goods.setIsMarketable("0");
            //登录用户id
            TbSeller seller = (TbSeller) SecurityUtils.getSubject().getPrincipal();
            goods.setSellerId(seller.getSellerId());

            //添加item表

            List<TbItem> items = JSON.parseArray(itemList,TbItem.class);
            for (TbItem item : items) {
                item.setCreateTime(new Date());
                item.setSeller(seller.getNickName());
                item.setSellerId(seller.getSellerId());
                item.setStatus("1");
                item.setUpdateTime(new Date());
            }
            goodsService.addGoodsAndGoodsDesc(goods,goodsDesc,items);
            return Result.perfect(ResultEnum.SUCCESS);
        } catch (Exception e){
            logger.error(String.valueOf(ResultEnum.FAIT),e);
            return Result.response(ResultEnum.FAIT);
        }
    }
    //oss上传图片
    @ResponseBody
    @RequestMapping("upload")
    public Result upload(@RequestParam("myFile")MultipartFile myFile){
        try {
            String upload = OSSutils.upload(myFile);
            return Result.success(ResultEnum.SUCCESS,upload);
        } catch (Exception e){
            logger.error(String.valueOf(ResultEnum.FAIT),e);
            return Result.response(ResultEnum.FAIT);
        }
    }
    //用模板表 specIds 字段中的id 查询规格表
    @ResponseBody
    @RequestMapping("getSpecificationOptionBySpecIds")
    public Result getSpecificationOptionBySpecIds(Long specId){
        try {
            List<TbSpecificationOption> specificationOptionList = specificationService.getSpecificationOptionBySpecIds(specId);
            return Result.success(ResultEnum.SUCCESS,specificationOptionList);
        } catch (Exception e){
            logger.error(String.valueOf(ResultEnum.FAIT),e);
            return Result.response(ResultEnum.FAIT);
        }
    }
    //批量删除,,,逻辑删除，修改删除字段
    @ResponseBody
    @RequestMapping("deleteGoods")
    public Result deleteGoods(@RequestParam(value = "ids[]")Long[] ids,Integer a){
        try {
            if (a == 10){
                //删除方法
                goodsService.deleteGoods(ids);
            }else if (a == 20){
                //审核方法
                goodsService.updateStatus(0,ids);
            }
            return Result.response(ResultEnum.SUCCESS);
        }catch (Exception e){
            logger.error(String.valueOf(ResultEnum.FAIT),e);
            return Result.response(ResultEnum.FAIT);
        }
    }
}
